package Server.Jsmj.RGCP;

import java.util.LinkedList;
import java.util.List;

public class RGCPHelper {
	
	public static void SortCard(int byHandCard[], int byHandMjCount) {
		for (int i = 0; i < byHandMjCount - 1; i++) {
			for (int j = i; j < byHandMjCount; j++) {
				int x = byHandCard[i];
				int y = byHandCard[j];

				if (x > y) {
					int tempvalue = byHandCard[i];
					byHandCard[i] = byHandCard[j];
					byHandCard[j] = tempvalue;
				}
			}
		}
	}

	public static int bai = 33;


	// ȥ���������ظ��ļ�¼
	public static Integer[] array_unique(int[] a) {
		// array_unique
		List<Integer> list = new LinkedList<Integer>();
		for (int i = 0; i < a.length; i++) {
			if (!list.contains(a[i])) {
				list.add(a[i]);
			}
		}
		return (Integer[]) list.toArray(new Integer[list.size()]);
	}

	public static int[] to_array(Integer[] a) {
		int allMj[] = new int[a.length];
		for (int i = 0; i < a.length; i++) {
			allMj[i] = a[i];
		}
		return allMj;
	}

	public static int GetPaiCount(int byHandMj[]) {
		int cnt = 0;
		for (int i = 0; i < Constants.HAND_MJ_MAX; i++) {
			if (byHandMj[i] != Constants.INVALID_CARD) {
				cnt++;
			}
		}
		return cnt;
	}

	public static int GetPaiCount(int byHandMj[], int byMj) {
		int cnt = 0;
		for (int i = 0; i < Constants.HAND_MJ_MAX; i++) {
			if (byHandMj[i] == byMj) {
				cnt++;
			}
		}
		return cnt;
	}

	public static boolean TestBit(int var, int bit) {
		return ((var) & (1 << (bit))) != 0;
	}

	// ����鵽���ƽṹ
	public static void AddBlock(HU hu, PTR_MJ_BLOCK block) {
		for (int i = 0; i < (Constants.HAND_MJ_MAX - 2) / 3; i++) {
			if (hu.block[i].style == Constants.BLOCK_NULL) {
				hu.block[i].first = block.first;
				hu.block[i].style = block.style;
				break;
			}
		}
	}

	public static void AddBlock(HU hu, BLOCK block) {
		for (int i = 0; i < (Constants.HAND_MJ_MAX - 2) / 3; i++) {
			if (hu.block[i].style == Constants.BLOCK_NULL) {
				hu.block[i].first = block.first;
				hu.block[i].style = block.style;
				break;
			}
		}
	}

	public static void AddBlock(HU hu, MJ_BLOCK block) {
		for (int i = 0; i < (Constants.HAND_MJ_MAX - 2) / 3; i++) {
			if (hu.block[i].style == Constants.BLOCK_NULL) {
				hu.block[i].first = block.first;
				hu.block[i].style = block.style;
				hu.block[i].count = block.count;
				break;
			}
		}
	}

	// �Ƿ����������
	public static boolean IsJiang(int byMj1, int byMj2) {
		if (byMj1 == byMj2 && (byMj1 < 42)) {
			return true;
		}
		return false;
	}

//	public static boolean IsTongHuaSe(int byMj1, int byMj21, int byMj31) {
//		int byMj2 = byMj21;
//		int byMj3 = byMj31;
//		if (byMj1 >= 0 && byMj1 <= 8 && byMj2 >= 0 && byMj2 <= 8 && byMj3 >= 0 && byMj3 <= 8) {
//			return true;
//		}
//		if (byMj1 >= 9 && byMj1 <= 17 && byMj2 >= 9 && byMj2 <= 17 && byMj3 >= 9 && byMj3 <= 17) {
//			return true;
//		}
//		if (byMj1 >= 18 && byMj1 <= 26 && byMj2 >= 18 && byMj2 <= 26 && byMj3 >= 18 && byMj3 <= 26) {
//			return true;
//		}
//		if ((byMj1 == byMj2) && (byMj2 == byMj3) && (byMj1 < 42)) {
//			return true;
//		}
//		return false;
//	}

	public static boolean IsTongHuaSe(int byMj1, int byMj2) {
		if (byMj1 <= 8 && byMj2 <= 8) {
			return true;
		}
		if (byMj1 >= 9 && byMj1 <= 17 && byMj2 >= 9 && byMj2 <= 17) {
			return true;
		}
		if (byMj1 >= 18 && byMj1 <= 26 && byMj2 >= 18 && byMj2 <= 26) {
			return true;
		}
		if ((byMj1 == byMj2) && (byMj1 < 34)) {
			return true;
		}
		return false;
	}

	// �Ƿ�������Ͳ
	public static boolean IsWST(int byMj1) {
		if (byMj1 <= 26) {
			return true;
		}
		return false;
	}

	public static boolean IsWan(int byMj1) {
		if (byMj1 <= 8) {
			return true;
		}
		return false;
	}

	public static boolean IsTong(int byMj1) {
		if (byMj1 > 8 && byMj1 <= 17) {
			return true;
		}
		return false;
	}

	public static boolean IsSuo(int byMj1) {
		if (byMj1 >= 18) {
			return true;
		}
		return false;
	}
	// �Ƿ��Ƕ����ϡ����������С�������
	public static boolean IsDNXBZFB(int byMj1) {
		if (byMj1 >= 27 && byMj1 <= 33) {
			return true;
		}
		return false;
	}
	// �Ƿ��Ƿ���
//	public static boolean IsFeng(int byMj1) {
//		if (byMj1 >= 27 && byMj1 <= 30) {
//			return true;
//		}
//		return false;
//	}

	// �Ƿ�����Ԫ
	public static boolean IsSanYuan(int byMj1) {
		if (byMj1 >= 31 && byMj1 <= 33) {
			return true;
		}
		return false;
	}
	/**
	 * ��ȡ��ǰ��ҵ���һ�����
	 * @param bySeatID
	 * @return
	 */
	public static int GetNextUser(int bySeatID) {
		return (bySeatID + 1) % RGCPTable.PLAYERCOUNT;
	}
	/**
	 * ��ȡ��ǰ��ҵ���һ�����
	 * @param bySeatID
	 * @return
	 */
	public static int GetPrevUser(int bySeatID) {
		return (bySeatID + 3) % RGCPTable.PLAYERCOUNT;
	}
	// �Ƿ��ǻ���
	public static boolean IsHuaPai(int byPai) {
		if (byPai >= 34 && byPai <= 38) {
			return true;
		}
		return false;
	}

	// �Ƿ��������
	public static boolean IsPeng(int byHandMj[], int byMj) {
		for (int i = 0; i < Constants.HAND_MJ_MAX - 2; i++) {
			if (byHandMj[i] == byMj && byHandMj[i + 1] == byMj) {
				return true;
			}
		}
		return false;
	}

	// �Ƿ�����
	public static MJ_GANGINFO IsGang(int byHandMj[], int byMj) {
		MJ_GANGINFO gangInfo = new MJ_GANGINFO();
		gangInfo.GangStyle = 0;
		gangInfo.GangMj = 0;
		// �������ϲ����е�ʮ������
		for (int i = 0; i < Constants.HAND_MJ_MAX - 3; i++) {
			if (byHandMj[i] == byMj && byHandMj[i + 1] == byMj && byHandMj[i + 2] == byMj) {
				gangInfo.GangMj = byHandMj[i];
				gangInfo.GangStyle = 1;
			}
		}
		return gangInfo;
	}

	public static MJ_GANGINFO IsZMGang(int byHandMj[], HU hu) {
		return IsZMGang(byHandMj, hu, 255, 255, 255);
	}

	// �������� ����,
	public static MJ_GANGINFO IsZMGang(int byHandMj[], HU hu, int jPos1, int jPos2, int jPos3) {
		MJ_GANGINFO gangInfo = new MJ_GANGINFO();
		gangInfo.GangStyle = 0;
		gangInfo.GangMj = Constants.INVALID_CARD;
		for (int i = 0; i < (Constants.HAND_MJ_MAX - 2) / 3; i++) {
			if (hu.block[i].style == Constants.BLOCK_NULL)
				break;
			else if (hu.block[i].style != Constants.BLOCK_PENG)
				continue;
			else {
				// �����������
				if (hu.block[i].first == jPos1 || hu.block[i].first == jPos2 || hu.block[i].first == jPos3)
					continue;
				for (int j = 0; j < Constants.HAND_MJ_MAX; j++) {
					// if( byHandMj[j]==jPos1 || byHandMj[j]==jPos2 )
					// continue;
					if (byHandMj[j] == hu.block[i].first) {
						gangInfo.GangStyle = 2; // ��������
						gangInfo.GangMj = byHandMj[j]; // ���ܺ��ƿ�λ
						return gangInfo;
					}
				}
			}
		}
		return gangInfo;
	}

	public static MJ_GANGINFO IsAnGang(int byHandMj[]) {
		return IsAnGang(byHandMj, 255, 255, 255);
	}

	public static MJ_GANGINFO IsAnGang(int byHandMj[], int jPos1) {
		return IsAnGang(byHandMj, jPos1, 255, 255);
	}

	public static MJ_GANGINFO IsAnGang(int byHandMj[], int jPos1, int jPos2) {
		return IsAnGang(byHandMj, jPos1, jPos2, 255);
	}
	
	public static MJ_LONGINFO IsAnLong(MajSide l_TmpMajSide,int byHandMj[], int fanJiang) {
		return IsAnLong(l_TmpMajSide,byHandMj, fanJiang, 255, 255, 255);
	}
	
	
	// �Ƿ�������
	public static MJ_LONGINFO IsAnLong(MajSide l_TmpMajSide,int byHandMj[], int fanJiang, int jPos2, int jPos3, int jPos4) {
		MJ_LONGINFO gangInfo = new MJ_LONGINFO();
		gangInfo.GangStyle = 0;
		gangInfo.GangMj = Constants.INVALID_CARD;
		for (int i = 0; i < Constants.HAND_MJ_MAX - 2; i++) {
			if (byHandMj[i] == fanJiang && byHandMj[i + 1] == fanJiang && byHandMj[i + 2] == fanJiang) {
				gangInfo.GangMj = byHandMj[i];
				gangInfo.GangStyle = 5;
				gangInfo.count = 3;
				return gangInfo;
			}
		}
		for (int i = 0; i < Constants.HAND_MJ_MAX - 3; i++) {
			// ����
			if (byHandMj[i] == jPos4 || byHandMj[i] == jPos2 || byHandMj[i] == jPos3)
				continue;
			if (byHandMj[i] == byHandMj[i + 1] && byHandMj[i] == byHandMj[i + 2] && byHandMj[i] == byHandMj[i+3]) {
					gangInfo.GangStyle = 5;
					gangInfo.GangMj = byHandMj[i];
					gangInfo.count = 4;
					return gangInfo;
			}
		}
		return gangInfo;
	}


	// �Ƿ񰵸�
	public static MJ_GANGINFO IsAnGang(int byHandMj[], int jPos1, int jPos2, int jPos3) {
		MJ_GANGINFO gangInfo = new MJ_GANGINFO();
		gangInfo.GangStyle = 0;
		gangInfo.GangMj = Constants.INVALID_CARD;
		for (int i = 0; i < Constants.HAND_MJ_MAX - 3; i++) {
			// ����
			if (byHandMj[i] == jPos1 || byHandMj[i] == jPos2 || byHandMj[i] == jPos3)
				continue;
			if (byHandMj[i] == byHandMj[i + 1] && byHandMj[i] == byHandMj[i + 2]) {
				// �����и���
				if (byHandMj[i] == byHandMj[i + 3]) {
					gangInfo.GangStyle = 4;
					gangInfo.GangMj = byHandMj[i];
				}
				// ����������
				else if (byHandMj[i] == byHandMj[Constants.HAND_MJ_MAX - 1]) {
					gangInfo.GangStyle = 4;
					gangInfo.GangMj = byHandMj[i];
				}
			}
		}
		return gangInfo;
	}

	// �����������
	public static void CleanHu(HU hu) {
		hu.jiangMj = Constants.INVALID_CARD;
		for (int i = 0; i < (Constants.HAND_MJ_MAX - 2) / 3; i++) {
			if (hu.block[i] == null) {
				hu.block[i] = new BLOCK();
			}
			hu.block[i].first = Constants.INVALID_CARD;
			hu.block[i].style = Constants.BLOCK_NULL;
		}
		for (int i = 0; i < Constants.HAND_MJ_MAX; i++) {
			hu.sanMj[i] = Constants.INVALID_CARD;
		}
	}

	// ������������
	public static void CopyHu(HU destHu, HU srcHu) {
		for (int i = 0; i < (Constants.HAND_MJ_MAX - 2) / 3; i++) {
			destHu.block[i].first = srcHu.block[i].first;
			destHu.block[i].style = srcHu.block[i].style;
		}
		for (int i = 0; i < Constants.HAND_MJ_MAX; i++) {
			destHu.sanMj[i] = srcHu.sanMj[i];
		}
	}

	public static void HuToHandMj(int byMjs[], HU srcHu) {
		int j = 0;
		for (int i = 0; i < (Constants.HAND_MJ_MAX - 2) / 3; i++) {
			switch (srcHu.block[i].style) {
			case Constants.BLOCK_NULL: {

			}
				break;
			case Constants.BLOCK_CHI: {
				byMjs[j++] = srcHu.block[i].first;
				byMjs[j++] = (srcHu.block[i].first + 1);
				byMjs[j++] = (srcHu.block[i].first + 2);
			}
				break;
			case Constants.BLOCK_SUN: // ˳��ṹ
			{
				byMjs[j++] = srcHu.block[i].first;
				byMjs[j++] = (srcHu.block[i].first + 1);
				byMjs[j++] = (srcHu.block[i].first + 2);
			}
				break;
			case Constants.BLOCK_PENG: // ���ӽṹ,3����ͬ
			{
				byMjs[j++] = srcHu.block[i].first;
				byMjs[j++] = srcHu.block[i].first;
				byMjs[j++] = srcHu.block[i].first;
			}
				break;
			case Constants.BLOCK_KE: // ���ӽṹ,3����ͬ,����
			{
				byMjs[j++] = srcHu.block[i].first;
				byMjs[j++] = srcHu.block[i].first;
				byMjs[j++] = srcHu.block[i].first;
			}
				break;
			case Constants.BLOCK_GANG: // ���ӽṹ,4����ͬ
			{
				byMjs[j++] = srcHu.block[i].first;
				byMjs[j++] = srcHu.block[i].first;
				byMjs[j++] = srcHu.block[i].first;
			}
				break;
			case Constants.BLOCK_AN_GANG: // ����
			{
				byMjs[j++] = srcHu.block[i].first;
				byMjs[j++] = srcHu.block[i].first;
				byMjs[j++] = srcHu.block[i].first;
			}
				break;
			}
		}
		int srcHujiangMj = srcHu.jiangMj;
		byMjs[j++] = srcHujiangMj;
		byMjs[j++] = srcHujiangMj;
	}

	public static int DeletePai(int byHandMj[], int byMj1) {
		return DeletePai(byHandMj, byMj1, 255, 255);
	}

	public static int DeletePai(int byHandMj[], int byMj1, int byMj2) {
		return DeletePai(byHandMj, byMj1, byMj2, 255);
	}

	public static int DeletePai(int byHandMj[], int byMj1, int byMj2, int byMj3) {
		int cnt = 0;
		for (int i = 0; i < Constants.HAND_MJ_MAX; i++) {
			if (byHandMj[i] == byMj1 && byMj1 != Constants.INVALID_CARD) {
				byHandMj[i] = Constants.INVALID_CARD;
				byMj1 = Constants.INVALID_CARD;
				cnt++;
			}
			if (byHandMj[i] == byMj2 && byMj2 != Constants.INVALID_CARD) {
				byHandMj[i] = Constants.INVALID_CARD;
				byMj2 = Constants.INVALID_CARD;
				cnt++;
			}
			if (byHandMj[i] == byMj3 && byMj3 != Constants.INVALID_CARD) {
				byHandMj[i] = Constants.INVALID_CARD;
				byMj3 = Constants.INVALID_CARD;
				cnt++;
			}
		}
		SortCard(byHandMj, Constants.HAND_MJ_MAX);
		return cnt;
	}

	public static void AdjustChiHu(HU hu, int byHuMj) {
		for (int i = 0; i < Constants.MAX_BLOCK_NUM; i++) {
			switch (hu.block[i].style) {
			case Constants.BLOCK_KE: {
				if (hu.block[i].first == byHuMj) {
					hu.block[i].style = Constants.BLOCK_PENG;
				}
			}
			default:
				break;
			}
		}
	}

	// ������������,���ط���
	public static long CheckGDHu(HU hu, int byFengWei, int byQuanWei, char strFang) {
		return 0;
	}

	public static BLOCK Peng(int byMjs[], int byMj, int caishen[]) {
		return Peng(byMjs, byMj, caishen[0]);
	}

	public static BLOCK Peng(int byMjs[], int byMj, int caishen) {
		if (byMj == Constants.bai) {
			int baiNum = 0;
			for (int i = 0; i < Constants.HAND_MJ_MAX - 1 && baiNum < 2; i++) {
				int mj = byMjs[i];
				if (mj == Constants.bai) {// ���Ȱװ�ȥ��
					byMjs[i] = Constants.INVALID_CARD;
					baiNum++;
				}
			}
			for (int i = 0; i < Constants.HAND_MJ_MAX - 1 && baiNum < 2; i++) {
				int mj = byMjs[i];
				if (mj == caishen) {// �����ò�����
					byMjs[i] = Constants.INVALID_CARD;
					baiNum++;
				}
			}
			MJ_BLOCK block = new MJ_BLOCK();
			block.first = byMj;
			block.style = Constants.BLOCK_PENG;
			return block;
		} else {
			return Peng(byMjs, byMj);
		}
	}

	public static BLOCK Peng(int byMjs[], int byMj) {
		MJ_BLOCK block = new MJ_BLOCK();
		for (int i = 0; i < Constants.HAND_MJ_MAX - 1; i++) {
			if (byMjs[i] == byMj && byMjs[i + 1] == byMj) {// ��ͨ��
				byMjs[i] = Constants.INVALID_CARD;
				byMjs[i + 1] = Constants.INVALID_CARD;
				// ����3��
				break;
			}
		}
		block.first = byMj;
		block.style = Constants.BLOCK_PENG;
		return block;
	}

	// �ܲ���
	public static BLOCK MGang(int byMjs[], int byMj) {
		MJ_BLOCK block = new MJ_BLOCK();
		block.style = Constants.BLOCK_NULL;
		block.first = -1;
		for (int i = 0; i < Constants.HAND_MJ_MAX - 3; i++) {
			if (byMjs[i] == byMj) {
				byMjs[i] = Constants.INVALID_CARD;
				byMjs[i + 1] = Constants.INVALID_CARD;
				byMjs[i + 2] = Constants.INVALID_CARD;
				block.style = Constants.BLOCK_GANG;
				block.first = byMj;
				// ����4��
				return block;
			}
		}
		return block;
	}

	// ��������-����
	public static void ZMGang(int byMjs[], HU hu, int GangMj) {
		for (int i = 0; i < (Constants.HAND_MJ_MAX - 2) / 3; i++) {
			if (hu.block[i].first == GangMj) {
				hu.block[i].style = Constants.BLOCK_GANG;
				break;// ����һ��
			}
		}
		for (int j = 0; j < Constants.HAND_MJ_MAX; j++) {
			if (byMjs[j] == GangMj) {
				byMjs[j] = Constants.INVALID_CARD;
				break;
			}
		}
	}

	// ����
	public static BLOCK AnGang(int byMjs[], int GangMj) {
		MJ_BLOCK block = new MJ_BLOCK();
		for (int i = 0; i < Constants.HAND_MJ_MAX; i++) {
			if (byMjs[i] == GangMj) {
				byMjs[i] = Constants.INVALID_CARD;
			}
		}
		block.style = Constants.BLOCK_AN_GANG;
		block.first = GangMj;
		return block;
	}
	
	//��
	public static BLOCK AnLong(int byMjs[], int GangMj) {
		MJ_BLOCK block = new MJ_BLOCK();
		int count = 0;
		for (int i = 0; i < Constants.HAND_MJ_MAX; i++) {
			if (byMjs[i] == GangMj) {
				byMjs[i] = Constants.INVALID_CARD;
				count++;
			}
		}
		block.style = Constants.BLOCK_TIANLong;
		block.first = GangMj;
		block.count = count;
		return block;
	}
}
